<?php


namespace app\admin\business;
use think\Exception;
use app\common\model\mysql\AdminUser as AdminUserModel;
class AddAuth
{
    public static function authority($data)
    {
        try{
            //查看数据库是否存在该用户名
            $message = AdminUserModel::getStatusAttrUsername($data['username']);
            if($message){
                throw new Exception('用户已存在!');
            }else{
                //登录时间登录ip用户密码操作人等信息写进数据库
                $addData = [
                    'last_time' => time(),
                    'last_ip' => request()->ip(),
                    'username' => trim($data['username']),
                    'status' => 2,
                    'password' => md5($data['repass']),
                    'operate_user' => session(config("session.session_admin"))['username']
                ];
                //静态方法用不了模型的save()只能使用对象
                $demo = new AdminUserModel;
                $res = $demo->AddAtuhUser($addData);
                if(empty($res)){
                    throw new Exception('写入异常!');
                }
            }

        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }

        return true;
    }
}